<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="performancecomparisonsList" @selection-change="handleSelectionChange">
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="分数范围" align="center">
        <template v-slot="scope">
          <!-- 情况一 -->
          <div v-if="Array.isArray(scope.row.scoreRanges) && scope.row.scoretype == 0">
            <div v-for="(range, index) in scope.row.scoreRanges" :key="index" class="score-range">
              <span>{{ range.start }} - {{ range.end }}: {{ range.description }}</span>
              <span v-if="index < scope.row.scoreRanges.length - 1">, </span>
            </div>
          </div>
          <!-- 情况三 -->
          <div v-if="scope.row.scoreRanges && scope.row.scoretype == 1">
            {{ scope.row.scoreRanges }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="描述信息" align="center" prop="descript" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['performancecomparisons:performancecomparisons:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改模板管理对话框1 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="performancecomparisonsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型" prop="type">
          <el-input v-model="form.type" disabled></el-input>
        </el-form-item>

        <el-form-item label="分数范围" prop="scoreRanges">
          <el-table :data="form.scoreRanges" style="width: 100%">
            <el-table-column prop="start" label="开始分数">
              <template #default="scope">
                <el-input v-model="scope.row.start" placeholder="开始分数"></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="end" label="结束分数">
              <template #default="scope">
                <el-input v-model="scope.row.end" placeholder="结束分数"></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="description" label="描述">
              <template #default="scope">
                <el-input v-model="scope.row.description" placeholder="描述"></el-input>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>

        <el-form-item label="描述信息" prop="type">
          <el-input v-model="form.descript"></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 添加或修改模板管理对话框2 -->
    <el-dialog :title="title" v-model="openn" width="500px" append-to-body>
      <el-form ref="performancecomparisonsReff" :model="formm" :rules="rules" label-width="80px">
        <el-form-item label="类型" prop="type">
          <el-input v-model="formm.type" disabled></el-input>
        </el-form-item>
        <el-form-item label="描述信息" prop="type">
          <el-input v-model="formm.descript"></el-input>
        </el-form-item>
        <el-form-item label="修改数据" prop="type">
          <el-input v-model="formm.scoreRanges"></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFormm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>


  </div>
</template>

<script setup name="Performancecomparisons">
import { listPerformancecomparisons, getPerformancecomparisons, delPerformancecomparisons, addPerformancecomparisons, updatePerformancecomparisons } from "@/api/performancecomparisons/performancecomparisons";

const { proxy } = getCurrentInstance();

const performancecomparisonsList = ref([]);
const open = ref(false);
const openn = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  formm: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    type: null,
    scoreRanges: null
  },
  rules: {
    type: [
      { required: true, message: "类型不能为空", trigger: "change" }
    ],
    scoreRanges: [
      { required: true, message: "分数范围不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules, formm } = toRefs(data);

/** 查询模板管理列表 */
function getList() {
  loading.value = true;
  listPerformancecomparisons(queryParams.value).then(response => {
    // 遍历每一行数据，解析 scoreRanges
    performancecomparisonsList.value = response.rows.map(item => {
      return {
        ...item,
        scoreRanges: JSON.parse(item.scoreRanges) // 解析 scoreRanges
      };
    });

    // console.log(performancecomparisonsList.value); // 打印解析后的数据
    total.value = response.total;
    loading.value = false;
  });
}


// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    type: null,
    scoreRanges: null
  };
  proxy.resetForm("performancecomparisonsRef");
  proxy.resetForm("performancecomparisonsReff");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加模板管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  // console.log(row)
  reset();
  this.title = "修改模板管理";
  // console.log(row.scoretype)
  if (row.scoretype == 0) {
    open.value = true
    this.form = { ...row }; // 深拷贝 row，确保不影响原始数据
  } else {
    openn.value = true
    this.formm = { ...row };
  }
}

/** 提交按钮 */

function submitForm() {
  var scoreRangesJson = JSON.stringify(form.value.scoreRanges);
  const updatedData = {
    ...form.value,
    scoreRanges: scoreRangesJson // 使用 JSON 字符串
  };

  if (form.value.id != null) {
    updatePerformancecomparisons(updatedData).then(response => {
      proxy.$modal.msgSuccess("修改成功");
      open.value = false;
      getList();
    });
  }
}

function submitFormm() {
  const updatedData = {
    id:formm.value.id,
    scoreRanges: JSON.stringify(formm.value.scoreRanges),
    descript:formm.value.descript,
  };
  
  if (formm.value.id != null) {
    updatePerformancecomparisons(updatedData).then(response => {
      console.log(response);

      proxy.$modal.msgSuccess("修改成功");
      openn.value = false;
      getList();
    });
  }
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除模板管理编号为"' + _ids + '"的数据项？').then(function () {
    return delPerformancecomparisons(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('performancecomparisons/performancecomparisons/export', {
    ...queryParams.value
  }, `performancecomparisons_${new Date().getTime()}.xlsx`)
}

getList();
</script>
